package info.empathymobile.monitor;

import info.empathymobile.monitor.business.MonitorBO;
import info.empathymobile.monitor.business.beans.MonitorEntry;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;

public class EmpathyMonitorController {
	
	private MonitorBO monitorBO = null;
	public final static String EMPATHY_MONITOR_SHARED_PREFERENCES = "EmpathyMonitor";
	public final static String LAST_CONNECTIVITY_STATUS_PREFERENCE = "LastConnectivityStatus";
	private static final String LAST_CONNECTIVITY_TYPE_PREFERENCE = "LastConnectivityType";
	public static final String DO_THE_BOOT_SCAN_PREFERENCE = "DoTheBootScan";
	private SharedPreferences sharedPref;
	private SharedPreferences.Editor sharedPrefEditor;
	
	public EmpathyMonitorController(Context context) {
		monitorBO = new MonitorBO(context);
		
		this.sharedPref = context.getSharedPreferences(EMPATHY_MONITOR_SHARED_PREFERENCES, Context.MODE_MULTI_PROCESS);
		sharedPrefEditor = this.sharedPref.edit();
	}
	
	public void initAsyncEmpathyMonitorScan(int logClass, int status){
		new EmpathyMonitorThread().execute(logClass, status);
	}
	
	public int deleteMonitorEntriesById(ArrayList<Integer> monitorEntriesId){
		return this.monitorBO.deleteMonitorEntriesById(monitorEntriesId);
	}
	
	public List<MonitorEntry> getAllMonitorEntries(){
		return this.monitorBO.getAllMonitorEntries();
	}
	
    public MonitorEntry getBatteryStatus(){
    	return this.monitorBO.getBatteryStatus();
    }
	
	public MonitorEntry getConnectivityStatus() {
		return this.monitorBO.getConnectivityStatus();
	}
	
	private MonitorEntry getLastConnectivityEntry(){
		MonitorEntry entry = new MonitorEntry();
		entry.setStatus(this.sharedPref.getInt(LAST_CONNECTIVITY_STATUS_PREFERENCE,-1));
		entry.setType(this.sharedPref.getInt(LAST_CONNECTIVITY_TYPE_PREFERENCE,-1));
		return entry;
	}
	
	private void rememberConnectivityEntry(MonitorEntry entry){
		this.sharedPrefEditor.putInt(LAST_CONNECTIVITY_STATUS_PREFERENCE, entry.getStatus());
		this.sharedPrefEditor.putInt(LAST_CONNECTIVITY_TYPE_PREFERENCE, entry.getType());
		this.sharedPrefEditor.commit();
	}
	
	private MonitorEntry connectivityScan() {
		MonitorEntry logEntry;
		logEntry = monitorBO.getConnectivityStatus();
		MonitorEntry lastConnectivityEntry = getLastConnectivityEntry();
		if(logEntry.getStatus() != lastConnectivityEntry.getStatus() 
				|| logEntry.getType() != lastConnectivityEntry.getType()){
			if(logEntry.getStatus() == MonitorEntry.CONNECTIVITY_ENTRY_NET_DISCONNECTED){
				logEntry.setType(lastConnectivityEntry.getType());
			}
			rememberConnectivityEntry(logEntry);
			
			return logEntry;
		}
		return null;
	}

	private MonitorEntry batteryScan(int status) {
		MonitorEntry logEntry;
		logEntry = monitorBO.getBatteryStatus();
		logEntry.setStatus(status);
		return logEntry;
	}
	
	public void bootScan() {
		monitorBO.saveMonitorEntry(connectivityScan());
		MonitorEntry batteryEntry = monitorBO.getBatteryStatus();
		if(batteryEntry.getType() > 0){
			batteryEntry.setStatus(MonitorEntry.BATTERY_ENTRY_POWER_CONNECTED);
		}
		else {
			batteryEntry.setStatus(MonitorEntry.BATTERY_ENTRY_POWER_DISCONNECTED);
		}
		monitorBO.saveMonitorEntry(batteryEntry);
		
		this.sharedPrefEditor.putBoolean(DO_THE_BOOT_SCAN_PREFERENCE, false);
		this.sharedPrefEditor.commit();
	}
	
	private class EmpathyMonitorThread extends AsyncTask<Integer, Void, Boolean> {
		@Override
		protected Boolean doInBackground(Integer... params) {
			MonitorEntry logEntry = null;
			
			if(params[0] == MonitorEntry.BATTERY_ENTRY_LOG_CLASS){
				logEntry = batteryScan(params[1]);
			}
			else if(params[0] == MonitorEntry.CONNECTIVITY_ENTRY_LOG_CLASS) {
				logEntry = connectivityScan();
			}
			else {
				logEntry = new MonitorEntry();
				logEntry.setLogClass(MonitorEntry.OS_ENTRY_LOG_CLASS);
				logEntry.setStatus(params[1]);
				
				if(logEntry.getStatus() == MonitorEntry.OS_ENTRY_BOOT){
					bootScan();
				}
			}
	 		
			if(logEntry != null){
				monitorBO.saveMonitorEntry(logEntry);
			}
        	return true;
		}
	}
}
